package user

import (
	"context"

	"github.com/zeromicro/go-zero/core/logc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"gitee.com/liuxuezhan/ar-platform/rpc-client/userServiceClient/userrpc"
	"github.com/zeromicro/go-zero/core/logx"
)

type UserVipLevelChangeLogListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserVipLevelChangeLogListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserVipLevelChangeLogListLogic {
	return &UserVipLevelChangeLogListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserVipLevelChangeLogListLogic) UserVipLevelChangeLogList(req *types.UserVipLevelChangeLogListRequest) (resp *types.UserVipLevelChangeLogListResponse, err error) {
	rpcReq := &userrpc.UserGetVipChangeLogsRequest{
		UserId:         req.UserId,
		OrderNo:        req.OrderNo,
		StartCreatedAt: req.StartTime,
		EndCreatedAt:   req.EndTime,
		Page:           req.Page,
		PageSize:       req.PageSize,
		LogType:        req.Status,
	}
	rpcResp, err := l.svcCtx.UserServiceRPC.UserGetVipChangeLog(l.ctx, rpcReq)
	if err != nil {
		logc.Error(l.ctx, "UserGetVipChangeLog error: %v", err)
		return &types.UserVipLevelChangeLogListResponse{
			UserVipLevelChangeLogs: make([]*types.UserVipLevelChangeLog, 0),
			Pagination:             &types.Pagination{},
		}, nil
	}
	resp = &types.UserVipLevelChangeLogListResponse{}
	logList := make([]*types.UserVipLevelChangeLog, 0)
	for _, log := range rpcResp.LogRecords {
		logList = append(logList, &types.UserVipLevelChangeLog{
			Id:        log.Id,
			UserId:    log.UserId,
			OrderNo:   log.OrderNo,
			LogType:   log.LogType,
			ClaimType: log.ClaimType,
			Exp:       log.Exp,
			TotalExp:  int32(log.TotalExp),
			CreatedAt: log.CreatedAt,
			// Detail:    log.Detail,
		})
	}
	pagination := &types.Pagination{
		Total:     rpcResp.Pagination.Total,
		Page:      rpcResp.Pagination.Page,
		PageSize:  rpcResp.Pagination.PageSize,
		TotalPage: rpcResp.Pagination.TotalPage,
	}
	resp.UserVipLevelChangeLogs = logList
	resp.Pagination = pagination

	return resp, nil

}
